package com.example.nonacademic_education.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaIgnore;
import com.example.nonacademic_education.constant.Constant;
import com.example.nonacademic_education.data.request.FileApplyVo;
import com.example.nonacademic_education.data.request.FileInfoR;
import com.example.nonacademic_education.data.request.ListData;
import com.example.nonacademic_education.data.request.UploadFileInfo;
import com.example.nonacademic_education.entity.FileApply;
import com.example.nonacademic_education.service.impl.FileServiceImpl;
import com.example.nonacademic_education.util.FileUtils;
import com.example.nonacademic_education.util.R;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author xinmiao
 * @description
 * @date 2023/4/18 11:17
 */
@RestController
@RequestMapping("file")
public class FileController {
    @Resource
    private FileServiceImpl fileService;
    @Resource
    private FileUtils fileUtils;
    @PostMapping("upload")
    @SaCheckPermission("file.upload")
    public R upload(@RequestBody MultipartFile file,Integer year,String projectName){
        return fileService.upload(file,year,projectName);
    }
    @PostMapping("apply")
    @SaCheckPermission("file.upload")
    public R apply(@RequestBody UploadFileInfo fileInfo){
        return fileService.apply(fileInfo);
    }
    @PutMapping("reload")
    @SaCheckPermission("file.upload")
    public R reload(Long id, @RequestBody MultipartFile file) {
        return fileService.reload(id,file);
    }
    @PostMapping("multiConditionQuery")
    @SaCheckPermission("file.query")
    public R multiConditionQuery(@RequestBody FileInfoR fileInfoR, Integer page, Integer size){
        return fileService.multiConditionQuery(fileInfoR,page,size);
    }
    @PostMapping("download")
    @SaCheckPermission("file.query")
    public void download(@RequestBody ListData ids, HttpServletResponse response, Long userId) throws IOException {
        fileService.download(ids.getIds(),response,userId);
    }
    @PostMapping("getApplyList")
    @SaCheckPermission("file.query")
    public R getApplyList(@RequestBody FileApplyVo fileApply, Long departmentId, Integer page, Integer size){
        return fileService.getApplyList(fileApply,departmentId,page,size);
    }
    @GetMapping("getFile")
    //@SaCheckPermission("file.query")
    //@SaCheckRole(Constant.STUDENT)
    @SaIgnore
    public void getFile(String path, HttpServletRequest request, HttpServletResponse response) throws IOException {
        fileUtils.getFile(path,request,response);//http://localhost:39001/file/getFile/2023%20office%E6%8A%80%E8%83%BD%E5%9F%B9%E8%AE%AD%20OA%E5%AE%A1%E6%89%B9%E5%8D%95.pdf
    }
    @GetMapping("downloadFile")
    //@SaCheckPermission("file.query")
    //@SaCheckRole(Constant.NORMAL_ADMIN)
    public void downloadFile(HttpServletResponse response,String filename) throws IOException {
        fileUtils.downloadFile(filename,response);
    }
}
